import $http from '@/ajax/http.js'

export const duizhang = {
	data() {
		return {
			token: wx.getStorageSync('token'),
			roles: wx.getStorageSync('roles'),
			unconfirmed: [],
			confirm: [],
			ids: ''
		}
	},
	methods: {
		changeType(newType) {
			const that = this
			that.type = newType;
			if (that.type == "account1") {
				that.weiduizhang();
			} else {
				that.yiduizhang();
			}
			console.log('当前type的值为：', that.type);
		},
		billDetails() {
			uni.navigateTo({
				url: "/pages/business/account/index"
			})
		},
		weiduizhang() {
			if (this.roles) {
				switch (this.roles) {
					case 6:
						this.storeWdz();
						break;
					case 5:
						this.businessWdz();
						break;
					case 7:
						this.serverWdz();
						break;
					case 4:
						this.cityWdz();
						break;
					case 3:
						this.causeWdz();
						break;
					default:
						break;
				}
			}
		},
		yiduizhang() {
			if (this.roles) {
				switch (this.roles) {
					case 6:
						this.storeYdz();
						break;
					case 5:
						this.businessYdz();
						break;
					case 7:
						this.serverYdz();
						break;
					case 4:
						this.cityYdz();
						break;
					case 3:
						this.causeYdz();
						break;
					default:
						break;
				}
			}
		},
		storeWdz() {
			$http.request({
				url: '/store/my/getUnBillList',
				method: 'GET',
			}).then((res) => {
				this.unconfirmed = res;
				this.ids = res.map(item => item.id);
			})
		},
		storeYdz() {
			$http.request({
				url: '/store/my/getBillList',
				method: 'GET',
			}).then((res) => {
				this.confirm = res;
				console.log(this.confirm, "已对账完成的数据");
			})
		},
		businessWdz() {
			$http.request({
				url: '/bus/index/getUnBillList',
				method: 'GET',
			}).then((res) => {
				this.unconfirmed = res;
				this.ids = res.map(item => item.id);
			})
		},
		businessYdz() {
			$http.request({
				header: {
					Authorization: this.token,
				},
				url: '/bus/index/getBillList',
				method: 'GET',
			}).then((res) => {
				this.confirm = res;
				console.log(this.confirm, "已对账完成的数据");
			})
		},
		serverWdz() {
			$http.request({
				url: "/waiter/my/getUnBillList",
				method: "GET"
			}).then((res) => {
				this.unconfirmed = res;
				this.ids = res.map(item => item.id);
			})
		},
		serverYdz() {
			$http.request({
				url: "/waiter/my/getBillList",
				method: "GET"
			}).then((res) => {
				this.confirm = res;
				console.log(this.confirm, "已对账完成的数据");
			})
		},
		cityWdz() {
			$http.request({
				url: '/city/index/getUnBillList',
				method: 'GET',
			}).then((res) => {
				this.unconfirmed = res;
				this.ids = res.map(item => item.id);
			})
		},
		cityYdz() {
			$http.request({
				url: '/city/index/getBillList',
				method: 'GET',
			}).then((res) => {
				this.confirm = res;
				console.log(this.confirm, "已对账完成的数据");
			})
		},

		causeWdz() {
			$http.request({
				url: '/work/index/getUnBillList',
				method: 'GET',
			}).then((res) => {
				this.unconfirmed = res;
				this.ids = res.map(item => item.id);
			})
		},

		causeYdz() {
			$http.request({
				url: '/work/index/getBillList',
				method: 'GET',
			}).then((res) => {
				this.confirm = res;
				console.log(this.confirm, "已对账完成的数据");
			})
		},



		getPackageValue(key, item, index) {
			const entries = this.unconfirmed[index].packageMap;
			return entries[key[0]];
		},
		getConfirm(key, item, index) {
			const confirms = this.confirm[index].packageMap;
			return confirms[key[0]];
		},
		confirmUnBillList(id) {
			if (this.roles) {
				switch (this.roles) {
					case 6:
						this.store(id);
						break;
					case 5:
						this.business(id);
						break;
					case 7:
						this.server(id);
						break;
					case 4:
						this.city(id);
						break;
					case 3:
						this.cause(id);
						break;
					default:
						break;
				}
			}
		},
		confirmUnBill(id, url) {
			wx.showModal({
				title: "提示",
				content: '您是否确定账单无误？',
				success: (res) => {
					if (res.confirm) {
						$http.request({
							header: {
								Authorization: this.token,
							},
							url: url,
							data: {
								id: id
							},
							method: 'POST',
						}).then((res) => {
							if (res.code == 200) {
								wx.showToast({
									title: '您已提交成功！',
								});
							} else {
								wx.showToast({
									title: '请求失败',
								});
							}
						});
					} else if (res.cancel) {
						wx.showToast({
							title: '您已取消',
						});
					}
				}
			});
		},
		business(id) {
			this.confirmUnBill(id, '/bus/index/confirmUnBillList');
		},
		store(id) {
			this.confirmUnBill(id, '/store/my/confirmUnBillList');
		},
		server(id) {
			this.confirmUnBill(id, '/waiter/my/confirmUnBillList');
		},
		city(id) {
			this.confirmUnBill(id, '/city/index/confirmUnBillList');
		},
		cause(id) {
			this.confirmUnBill(id, '/work/index/confirmUnBillList');
		}
	}
}